package oop.controller.action.mark;

import java.util.List;

import oop.controller.action.DefaultAction;
import oop.data.Mark;
import oop.db.dao.MarkDAO;
import oop.taglib.UtilFunctions;

import org.apache.commons.lang.StringUtils;

public class ListAction extends DefaultAction {

	public static final int PAGE_LENGTH = 30;

	@Override
	public void performImpl() throws Exception {
		title("Danh sách bài chấm");
		String pageStr = StringUtils.defaultIfEmpty(request
				.getParameter("page"), "1");
		long page = Integer.parseInt(pageStr);
		long examId = Long.parseLong(request.getParameter("ml_examid"));

		List<Mark> marks = MarkDAO.fetchByExamination(examId, (page - 1)
				* PAGE_LENGTH, PAGE_LENGTH);
		;
		long count = MarkDAO.countByExamination(examId);

		request.setAttribute("marks", marks);
		request.setAttribute("page", page);
		request.setAttribute("pageCount", UtilFunctions.ceil(count / (double)PAGE_LENGTH));
	}

}
